module.exports = app => {
    const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;

    const Order = app.model.define('order', {
        id: {
            type: INTEGER(20),
            primaryKey: true,
            autoIncrement: true
        },
        no: {
            type: STRING(100),
            allowNull: false,
            defaultValue: '',
            comment: '订单号',
            unique: true
        },
        user_id: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '用户id',
            references: {
                model: 'user',
                key: 'id'
            },
            onDelete: 'cascade',
            onUpdate: 'restrict', // 更新时操作
        },
        type: {
            type: STRING(50),
            allowNull: false,
            comment: '商品类型 1课程 2测评'
        },
        commodityid: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '商品id'
        },
        commodityimg: {
            type: STRING(500),
            allowNull: false,
            comment: '商品图片'
        },
        commodityname: {
            type: STRING(50),
            allowNull: false,
            comment: '商品名称'
        },
        price: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '价格'
        },
        status: {
            type: ENUM,
            values: ['pending', 'success', 'fail'],
            allowNull: false,
            defaultValue: 'pending',
            comment: '支付状态'
        },
        created_at: DATE,
        updated_at: DATE,
    });

    // 关联关系
    Order.associate = function(models) {
        // 关联用户
        Order.belongsTo(app.model.User);
    }

    return Order;
};